(function() {

	var E = window.wangEditor
	var editor = new E('#editor')
	// 或者 var editor = new E( document.getElementById('editor') )
	editor.create()

	layui.use(['table', 'element', 'form', 'layer', 'laydate'], function() {
		var table = layui.table;
		var element = layui.element;
		var form = layui.form;
		var layer = layui.layer;
		var laydate = layui.laydate;
		var $ = layui.jquery;
		var url = GlobalSiteUrl + "/manage/selectNews";

		//日期范围
		laydate.render({
			elem: '#date',
			range: true,
			done: function(value, date, endDate) {
				var beginTime = value.substring(0, 10)
				var endTime = value.substring(13)
				if (beginTime && endTime) {
				table.reload('reloadTable', {
					page: {
						curr: 1 //重新从第 1 页开始
					},
					where: {
						beginTime: new Date(beginTime),
						endTime: new Date(endTime)
						},
					}, "data");
				} else {
					location.reload()
				}
			}
		});

		//第一个实例
		table.render({
			elem: '#table',
			toolbar: '#toolbarDemo',
			width: 'auto',
			height: 'full - 100',
			url: url, //数据接口
			response: {
				statusCode: 200 //重新规定成功的状态码为 200，table 组件默认为 0
			},
			parseData: function(res) {
				console.log(res)
				if (res.data == null) {
					alert(res.msg)
					location.reload();
					return;
				} else {
					let data = res.data.list
						data.forEach(element => {
							element['create_time'] = formatTime(element['create_time'])
							element['update_time'] =	!element['update_time'] ? '暂未更新' : formatTime(element['update_time'])
						});
					if (!res.data.total) {
						return {
							"code": res.status,
							"msg": res.msg,
							"data": data,
							"count": 1
						}
					} else {
						return {
							"code": res.status,
							"msg": res.msg,
							"data": data,
							"count": res.data.total
						}
					}
				}
			},
			page: true, //开启分页
			method: 'get',
			id: 'reloadTable',
			cols: [
				[ //表头
					// {
					// 	type: 'checkbox',
					// 	fixed: 'left',
					// },
					{
						fixed: 'left',
						field: 'nid',
						title: 'id',
						width: 60,
						align: 'center',
						sort: true
					},
					{
						field: 'title',
						title: '标题',
						align: 'center',
						width: 300,
					},
					{
						field: 'text',
						title: '描述',
						align: 'center',
						width: 130
					},
					{
						field: 'image',
						title: '图片',
						align: 'center',
						width: 160,
						templet: '#img'
					},
					{
						field: 'name',
						title: '发布者',
						align: 'center',
						width: 120,
					},
					{
						field: 'create_time',
						title: '创建时间',
						align: 'center',
						width: 160,
						sort: true
					},{
						field: 'update_time',
						title: '更新时间',
						align: 'center',
						width: 160,
						sort: true
					},
					{
						fixed: 'right',
						align: 'center',
						title: '操作',
						toolbar: '#barDemo',
						width: 180
					}
				]
			]
		});

		//监听行工具事件
		table.on('tool(table)', function(obj) {
			var data = obj.data;
			var nid = data.nid;
			if (obj.event === 'img') {
				var img = "<img src='" + data.image + "' class='img' style='width:98%;margin-left: 5px;'>"
				var image = "<div class='" + "'id='" + "demo2" + "'>" + img + "</div>"
				layer.open({
					area: ['600px', '500px'],
					type: 1,
					title: "查看图片",
					value: data.image,
					content: image,
					btn: '关闭',
					yes: function(index) {
						layer.closeAll();
					}
				});
			}
			if (obj.event === 'update') {
				$("title").attr("title",data.title)
				$("text").attr("text",data.text)
				layer.open({
					area: ['780px', '500px'],
					type: 1,
					title: '修改id为' + data.nid + '资讯的信息',
					content: $('.addPanel'),
					btn: ['确定', '取消'],
					yes: function(index) {
						var data = $('.addForm').serializeArray();
						var url = GlobalSiteUrl + "/manage/updateNews";
						var html = editor.txt.html()
						var text = editor.txt.text()
						if (text == '' || text == null) {
							html = null;
						}
						var suid = localStorage.getItem('suid');
						if (data[0].value == "" && text == "") {
							layer.msg("至少修改一项", {
								time: 1500,
								anim: 6
							});
							return 0;
						}
						$.post({
							url: url,
							data: {
								nid: nid,
								title: data[0].value,
								text: html
							},
							success: function(res) {
								if (res.status == 200) {
									layer.msg(res.msg);
									setTimeout(function() {
										location.reload();
									}, 1000);
									layer.close(index);
								} else {
									layer.msg(res.msg, {
										time: 1500,
										anim: 6
									});
									location.reload();
									layer.close(index);
								}
							}
						});
					}
				});

			} else if (obj.event === 'delete') {
				var url = GlobalSiteUrl + "/manage/updateNews?nid=" + obj.data.nid + "&delState=1";
				layer.confirm(
					"确认删除？", {
						btn: ['确定', '取消'],
						title: "提示"
					},
					function(index) {
						$.post({
							url: url,
							success: function(res) {
								if (res.status == 200) {
									layer.msg("删除成功");
									layer.close(index);
									setTimeout(function() {
										location.reload();
									}, 1000);
								} else {
									layer.msg("删除失败", {
										time: 1500,
										anim: 6
									});
									layer.close(index);
								}
							}
						});
					});
			}
		});

		// 按键监听
		$('.demoTable .layui-btn').on('click', function() {
			var type = $(this).data('type');
			active[type] ? active[type].call(this) : '';
		});

	});
})();
